package com.xyht.sca_s.student_manage_system.modules.produtProcurement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.entity.SmsProductProcurementApprovalProcess;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.entity.dto.SmsProductProcurementProcessDto;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.entity.vo.SmsProductProcurementProcessVo;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.entity.vo.SmsProductProcurementUserVo;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.mapper.SmsProductProcurementApprovalProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.produtProcurement.service.SmsProductProcurementApprovalProcessService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.produtProcurement.constant.ProcessObjectTypeConstant.PRODUCT_PROCUREMENT_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.produtProcurement.constant.ProcessObjectTypeConstant.PRODUCT_PROCUREMENT_OBJECT_TYPE_ROLE;

/**
 * <p>
 * 大宗商品审批流程表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-22
 */
@Service
public class SmsProductProcurementApprovalProcessServiceImpl extends ServiceImpl<SmsProductProcurementApprovalProcessMapper, SmsProductProcurementApprovalProcess> implements SmsProductProcurementApprovalProcessService {

    @Resource
    private SmsProductProcurementApprovalProcessMapper processMapper;
    @Resource
    private CacheUtil cacheUtil;
    @Resource
    private SmsRoleMapper smsRoleMapper;

    @Override
    public ResponseResult updateProductProcess(SmsProductProcurementProcessDto processDto) {
        if(isNullOrEmpty(processDto.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if(isNullOrEmpty(processDto.getProcessObjectType())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //如果在可编辑情况下
        //审批对象类型不为空时,审批对象id可为空
        if (processDto.getProcessIsApplicantEdit()) {
            if (processDto.getProcessObjectType() == PRODUCT_PROCUREMENT_OBJECT_TYPE_ROLE) {
                if (isNullOrEmpty(processDto.getProcessObjectId())) {
                    return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
                }
            }
        } else {
            if (isNullOrEmpty(processDto.getProcessObjectId())) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
        }

        SmsProductProcurementApprovalProcess approvalProcess = processMapper.selectById(processDto.getId());
        //判断审批流程是否存在
        if(approvalProcess == null || isNullOrEmpty(approvalProcess.getId())){
            return CommonResult.failed(CommonCodeEnum.PRODUCT_PROCUREMENT_PROCESS_NOT_EXIST);
        }
        //判断角色或个人是否存在
        if(processDto.getProcessObjectType() != null && !isNullOrEmpty(processDto.getProcessObjectId())){
            if(processDto.getProcessObjectType() == PRODUCT_PROCUREMENT_OBJECT_TYPE_PERSON){
                SmsUser userInfo = cacheUtil.getUserInfo(processDto.getProcessObjectId());
                if(userInfo == null || isNullOrEmpty(userInfo.getId())){
                    return CommonResult.failed(CommonCodeEnum.PRODUCT_PROCUREMENT_PROCESS_USER_NOT_EXIST);
                }
            }else if(processDto.getProcessObjectType() == PRODUCT_PROCUREMENT_OBJECT_TYPE_ROLE){
                SmsRole smsRole = smsRoleMapper.selectById(processDto.getProcessObjectId());
                if(smsRole == null || isNullOrEmpty(smsRole.getId())){
                    return CommonResult.failed(CommonCodeEnum.PRODUCT_PROCUREMENT_PROCESS_ROLE_NOT_EXIST);
                }
            }
        }
        SmsProductProcurementApprovalProcess updateProcess = new SmsProductProcurementApprovalProcess();
        updateProcess.setId(processDto.getId());
        if (!isNullOrEmpty(processDto.getProcessObjectType())) {
            updateProcess.setProcessObjectId(processDto.getProcessObjectId());
            updateProcess.setProcessObjectType(processDto.getProcessObjectType());
        }
        if (!isNullOrEmpty(processDto.getProcessIsApplicantEdit())) {
            updateProcess.setProcessIsApplicantEdit(processDto.getProcessIsApplicantEdit());
        }
        if (!isNullOrEmpty(processDto.getProcessIsSign())) {
            updateProcess.setProcessIsSign(processDto.getProcessIsSign());
        }
        if (!isNullOrEmpty(processDto.getProcessIsStamp())) {
            updateProcess.setProcessIsStamp(processDto.getProcessIsStamp());
        }
        if (!isNullOrEmpty(processDto.getProcessIsUse())) {
            updateProcess.setProcessIsUse(processDto.getProcessIsUse());
        }

        processMapper.updateById(updateProcess);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getProductProcessList() {
        List<SmsProductProcurementProcessVo> procurementProcessVoList =
                processMapper.selectList(new QueryWrapper<SmsProductProcurementApprovalProcess>()
                        .lambda()
                        .orderByAsc(SmsProductProcurementApprovalProcess::getProcessSort))
                        .stream()
                        .map(process -> {
                            SmsProductProcurementProcessVo processVo =
                                    new SmsProductProcurementProcessVo();
                            BeanUtils.copyProperties(process, processVo);

                            if (process.getProcessObjectType() != null && !isNullOrEmpty(process.getProcessObjectId())) {
                                if (process.getProcessObjectType() == PRODUCT_PROCUREMENT_OBJECT_TYPE_PERSON) {
                                    SmsUser smsUser = cacheUtil.getUserInfo(process.getProcessObjectId());
                                    if (smsUser != null && !isNullOrEmpty(smsUser.getId())) {
                                        SmsProductProcurementUserVo userVo = new SmsProductProcurementUserVo();
                                        BeanUtils.copyProperties(smsUser,userVo);
                                        processVo.setProcessObjectName(smsUser.getRealName());
                                        processVo.setUserInfo(userVo);
                                    }
                                } else {
                                    SmsRole smsRole = smsRoleMapper.selectById(process.getProcessObjectId());
                                    if (smsRole != null && !isNullOrEmpty(smsRole.getId())) {
                                        processVo.setProcessObjectName(smsRole.getRoleName());
                                    }
                                }
                            }
                            return processVo;
                        })
                        .collect(Collectors.toList());
        return CommonResult.success(procurementProcessVoList);
    }
}
